import { cloneDeep, } from 'lodash-es';
import dicts from './dict.json'

let dictList: DictRecord[] = dicts;

let mocks: MockRecord[] = [
  {
    method: "get",
    url: '/api/system/dict/type/optionselect',
    response() {
      let resDict = cloneDeep(dicts);
      resDict.forEach(item => {
        delete item.children;
      })
      let resData: responseType = {
        code: 200,
        data: resDict,
        msg: "",
      }
      return resData;
    }
  },
  {
    method: "get",
    url: '/api/system/dict/data/type/:dictType',
    response(response) {
      let dictItem = dicts.find(item => item.dictType == response.query.dictType);
      let resData: responseType = {
        code: 200,
        data: dictItem?.children || [],
        msg: "",
      }
      return resData;
    }
  },
  {
    method: "get",
    url: "/api/system/dict/type/list",
    response(option) {
      const copyList = cloneDeep(dictList);

      let resList = dictList;

      if (option.query.pageNum) {
        resList = copyList.splice((option.query.pageNum - 1) * option.query.pageSize, option.query.pageSize);
      }

      const resData: responseType = {
        code: 200,
        msg: "查询成功",
        total: dictList.length,
        ...(option.query.pageNum ? { rows: resList } : { data: resList })
      }

      return resData;
    }
  },
  {
    method: "get",
    url: "/api/system/dict/type/:dictId",
    response(option) {
      const resDict = dictList.find(item => item.dictId == option.query.dictId);

      const resData: responseType = {
        code: resDict ? 200 : 500,
        msg: resDict ? "查询成功" : "查询失败",
        data: resDict,
      }

      return resData;
    }
  },
  {
    method: "post",
    url: "/api/system/dict/type",
    response(option) {
      const newDict = option.body;

      newDict.dictId = dictList.length + 1;
      dictList.unshift(newDict);

      const resData: responseType = {
        code: 200,
        msg: "新增成功",
      }

      return resData;
    }
  },
  {
    method: "put",
    url: "/api/system/dict/type",
    response(option) {
      const newDict = option.body;

      const index = dictList.findIndex(item => item.dictId == newDict.dictId);

      dictList.splice(index, 1, newDict);

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
  {
    method: "delete",
    url: "/api/system/dict/type/:dictIds",
    response(option) {
      const dictIds = option.query.dictIds.split(',');

      const filterList = dictList.filter(item => !dictIds.includes(String(item.dictId)));

      dictList = filterList;

      const resData: responseType = {
        code: 200,
        msg: "删除成功",
      }
      return resData;
    }
  }
]

export default mocks